﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key">

  
  
  <!-- The current sprint number -->
  <xsl:variable name="currentSprintNumber" select="/ProjectHourBankDataSet/Project/Sprint[SprintUId = /ProjectHourBankDataSet/Project/CurrentSprintUId]/SprintNumber"/>


  <xsl:variable name="ServerURL" select="/ProjectHourBankDataSet/General/ServerURL"/>

  <!-- Sprints that had gone -->
  <xsl:variable name="PastSprints" select="/ProjectHourBankDataSet/Project/Sprint[SprintNumber &lt;= $currentSprintNumber]"/>


  <xsl:variable name="plannedUnits"
                 select="/ProjectHourBankDataSet/Project/BacklogItemUnit[UnitName = /ProjectHourBankDataSet/Project/BacklogItem/BacklogItemHours[Quantity &gt; 0]/UnitName]"/>

  <xsl:variable name="usedUnits"
                 select="/ProjectHourBankDataSet/Project/BacklogItemUnit[UnitName = /ProjectHourBankDataSet/PayableTasks[Effort &gt; 0]/UnitName]"/>

  <xsl:variable name="showUnits"
                 select="/ProjectHourBankDataSet/Project/BacklogItemUnit[UnitName = $plannedUnits/UnitName or UnitName = $usedUnits/UnitName]"/>



  <!-- Key for join tasks by name -->
  <xsl:key name="Task-by-TaskName" match="PayableTasks" use="concat(TaskName,'_',BacklogItemUId)" />
  
  <!-- Key for child projects  -->
  <xsl:key name="ChildProject-by-ProjectUId" match="ProjectHourBankChildProjects" use="ProjectUId" />

  <xsl:key name="Hours_By_BaseLine" match="BacklogItemHours" use="BaseLine"/>
  <xsl:key name="Sprint_By_SprintUId" match="Sprint" use="SprintUId"/>


  <xsl:include href="../../Style.xslt"/>


  <!-- BacklogItemHeader ******************************************* -->
  <!-- Shows the Scenario Header for a given scnearioUId, with    -->
  <!-- its name, status, and units.                               -->
  <!-- If param onlyEffort is true, doesnt show planned hours.    -->
  <!-- ***********************************************************-->
  <xsl:template name="BacklogItemHeader">
    <xsl:param name="backlogItemUId"/>
    
    <TableRow>
      <TableCell ColumnSpan="5" Style="{{StaticResource NoBorderCell}}">
        <Paragraph Style="{{StaticResource TitleParagraph}}" >
          [<xsl:value-of select="format-number(BacklogItemNumber,'000','pt-br-Decimal')"/>]
          <xsl:value-of select="Title"/>          
        </Paragraph>
      </TableCell>

    
    </TableRow>

  </xsl:template>
 
  <!-- TaskHeader *********************************************** -->
  <!-- Shows the Task Header.                                     -->
  <!-- ***********************************************************-->
  <xsl:template name="TaskHeader">
    <TableRow>
      <TableCell   Style="{{StaticResource DoubleLineCell}}">
        <Paragraph TextAlignment="Right">
          <Bold>#</Bold>
        </Paragraph>
      </TableCell>
      <TableCell   Style="{{StaticResource DoubleLineCell}}">
        <Paragraph>
          <Bold>Task</Bold>
        </Paragraph>
      </TableCell>
      <TableCell   Style="{{StaticResource DoubleLineCell}}">
        <Paragraph TextAlignment="Center">
          <Bold>Status</Bold>
        </Paragraph>
      </TableCell>
      <TableCell   Style="{{StaticResource DoubleLineCell}}">
        <Paragraph TextAlignment="Right">
          <Bold>Start</Bold>
        </Paragraph>
      </TableCell>
      <TableCell   Style="{{StaticResource DoubleLineCell}}">
        <Paragraph TextAlignment="Right">
          <Bold>End</Bold>
        </Paragraph>
      </TableCell>
      <xsl:for-each select="$usedUnits">
        <xsl:sort select="UnitName" />
        <TableCell   Style="{{StaticResource DoubleLineCell}}">
          <Paragraph TextAlignment="Right">
            <Bold>
              <xsl:value-of select="UnitName"/>
            </Bold>
          </Paragraph>
        </TableCell>
      </xsl:for-each>
    </TableRow>
  </xsl:template>

  <!-- BacklogItemSum ********************************************** -->
  <!-- Shows the Scenario Sum for a given scnearioUId.            -->
  <!-- If param onlyEffort is true, only the effort is show and   -->
  <!-- not the balance.                                           -->
  <!-- ***********************************************************-->
  <xsl:template name="BacklogItemSum">    
    <xsl:param name="backlogItemUId"/>
    <xsl:param name="backlogItemNumber"/>
    <TableRow>
      <TableCell Style="{{StaticResource NoBorderCell}}" ColumnSpan="5">
        <Paragraph Style="{{StaticResource SumItemParagraph}}">
          Total
        </Paragraph>
      </TableCell>
      <xsl:for-each select="$usedUnits">
        <xsl:sort select="UnitName" />
        <xsl:variable name="unitName" select="UnitName"/>
        <xsl:variable name="effort" select="sum(/ProjectHourBankDataSet/PayableTasks[UnitName=$unitName and BacklogItemUId=$backlogItemUId]/Effort)"/>
        <TableCell Style="{{StaticResource NoBorderCell}}">
          <Paragraph Style="{{StaticResource SumItemParagraph}}">
            <xsl:value-of select ="format-number($effort,'#.##0,00','pt-br-Decimal')"/>
          </Paragraph>
        </TableCell>

      </xsl:for-each>
    </TableRow>
    
      
  </xsl:template>

  <!-- TaskWithoutUnitAlert************************************** -->  
  <!-- ***********************************************************-->
  <xsl:template name="TaskWithoutUnitAlert">

    <xsl:variable name="taskUnits" select="/ProjectHourBankDataSet/PayableTasks/UnitName"/>
    <xsl:variable name="projectUnits" select="/ProjectHourBankDataSet/Project/BacklogItemUnit[Type != 6]/UnitName"/>
    <xsl:variable name="differUnits" select="$taskUnits[not(text()=$projectUnits)]"/>

    <xsl:if test="count($differUnits)>0">

      <BlockUIContainer>
        <Border Style="{{StaticResource AlertBorder}}" >
          <TextBlock FontSize="12">
            <Bold>WARNING</Bold>
            <LineBreak/>
            Some tasks were assigned to team members with roles that are not available at the project planned resources.<LineBreak/>
            Those tasks are signed in yellow. Please, change their roles.
          </TextBlock>
        </Border>
      </BlockUIContainer>

    </xsl:if>
        
  </xsl:template>

  <!-- BacklogItemTasks *******************************************  -->
  <!-- Shows all tasks from a given ScenarioNumber.               -->
  <!-- If two tasks has the same name they are group toguether.   -->
  <!-- For each tasks, shows its units and hours spent.           -->
  <!-- ***********************************************************-->
  <xsl:template name="BacklogItemTasks">
    <xsl:param name="backlogItemUId"/>
        
    <xsl:for-each select="/ProjectHourBankDataSet/PayableTasks[ count(. | key('Task-by-TaskName', concat(TaskName,'_',BacklogItemUId))[1]) = 1 and BacklogItemUId = $backlogItemUId]">

      <xsl:sort select="TaskId" data-type="number" order="ascending" />

      <xsl:variable name="taskName" select="TaskName"/>

      <!-- descubro a interscção das unidades da tarefa com as unidades do projeto-->
      <xsl:variable name="taskUnits" select="/ProjectHourBankDataSet/PayableTasks[TaskName=$taskName]/UnitName"/>
      <xsl:variable name="projectUnits" select="/ProjectHourBankDataSet/Project/BacklogItemUnit[Type != 6]/UnitName"/>
      <xsl:variable name="interUnits" select="$taskUnits[text()=$projectUnits]"/>
      <TableRow>
        <TableCell Style="{{StaticResource SingleLineCell}}" >
          <Paragraph TextAlignment="Right">
            <xsl:for-each select="/ProjectHourBankDataSet/PayableTasks[TaskName=$taskName and BacklogItemUId=$backlogItemUId]">
              <xsl:value-of select="TaskId"/>
              <xsl:if test="position()!=last()">
                <LineBreak/>
              </xsl:if>
            </xsl:for-each>
          </Paragraph>
        </TableCell>
        <xsl:element name="TableCell">
          <xsl:attribute name="Style">
            <xsl:choose>
              <xsl:when test="count($interUnits) &lt; count($taskUnits)">{StaticResource TaskAlertCell}</xsl:when>
              <xsl:otherwise>{StaticResource SingleLineCell}</xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <Paragraph>
            <xsl:value-of select="TaskName"/>
          </Paragraph>
        </xsl:element>
        <TableCell Style="{{StaticResource SingleLineCell}}" >
          <Paragraph TextAlignment="Center">
            <xsl:choose>
              <xsl:when test ="Status='0'">
                Registered
              </xsl:when>
              <xsl:when test ="Status='1'">
                Required
              </xsl:when>
              <xsl:when test ="Status='2'">
                Working
              </xsl:when>
              <xsl:when test ="Status='3'">
                Testing
              </xsl:when>
              <xsl:when test ="Status='4'">
                Done
              </xsl:when>
              <xsl:when test ="Status='5'">
                Canceled
              </xsl:when>
            </xsl:choose>
          </Paragraph>
        </TableCell>
        <TableCell Style="{{StaticResource SingleLineCell}}">
          <Paragraph TextAlignment="Right">
            <xsl:value-of select="StartString"/>
          </Paragraph>
        </TableCell>
        <TableCell Style="{{StaticResource SingleLineCell}}">
          <Paragraph TextAlignment="Right">
            <xsl:choose>
              <xsl:when test="FinishString!=''">
                <xsl:value-of select="FinishString"/>
              </xsl:when>
              <xsl:otherwise>
                &#160;
              </xsl:otherwise>
            </xsl:choose>
          </Paragraph>
        </TableCell>

        <!-- Para cada Unidade de Item de Backlog -->
        <xsl:for-each select="$usedUnits">
          <xsl:sort select="UnitName" />
          <xsl:variable name="unitName" select="UnitName"/>
          <xsl:variable name="effort" select="sum(/ProjectHourBankDataSet/PayableTasks[TaskName=$taskName and UnitName=$unitName and BacklogItemUId=$backlogItemUId]/Effort)"/>
          <xsl:variable name="isThere" select="count(/ProjectHourBankDataSet/PayableTasks[TaskName=$taskName and UnitName=$unitName and BacklogItemUId=$backlogItemUId]/Effort)"/>

          <xsl:element name="TableCell">
            <xsl:attribute name="Style">
              <xsl:choose>
                <xsl:when test="$effort='0' and $isThere>0">{StaticResource TaskAlertCell}</xsl:when>
                <xsl:otherwise>{StaticResource SingleLineCell}</xsl:otherwise>
              </xsl:choose>              
            </xsl:attribute>

            <Paragraph TextAlignment="Right">
              <xsl:choose>
                <xsl:when test="$isThere>0">                  
                  <xsl:value-of select="format-number($effort,'#.##0,0','pt-br-Decimal')"/>
                </xsl:when>
                <xsl:otherwise>
                  &#160;
                </xsl:otherwise>
              </xsl:choose>
            </Paragraph>


          </xsl:element>


        </xsl:for-each>

      </TableRow>
    </xsl:for-each>
  </xsl:template>

 
 
 

 
 
  <xsl:template name="ColumnDefinition">
    <Table.Columns>
      <TableColumn Width="40" />
      <TableColumn Width="300" />
      <TableColumn Width="60" />
    </Table.Columns>
  </xsl:template>



  <xsl:template name="ItemBalanceScore">
    <xsl:param name="score"/>
    <xsl:variable name="bgColor">
      <xsl:choose>
        <xsl:when test="$score &gt;=0">#77A44C</xsl:when>
        <xsl:otherwise>#d47777</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="fgColor">
      <xsl:choose>
        <xsl:when test="$score &gt;=0">#416000</xsl:when>
        <xsl:otherwise>#820909</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <BlockUIContainer>
      <StackPanel Orientation="Vertical">
        <Border CornerRadius="5,5,5,5" Background="{$bgColor}"  BorderBrush="{$fgColor}" BorderThickness="2" Padding="5" Margin="3,15,3,2">
          <TextBlock FontSize="30" Foreground="{$fgColor}" HorizontalAlignment="Center">
            <Bold>
              <xsl:value-of select="format-number($score,'#.##0','pt-br-Decimal')"/>
            </Bold>
          </TextBlock>
        </Border>
        <TextBlock FontSize="14" Foreground="{$fgColor}" HorizontalAlignment="Center">
          <Bold>Hours</Bold>
        </TextBlock>
      </StackPanel>

    </BlockUIContainer>
  </xsl:template>

  <xsl:template name="ItemPlanDone">
    <xsl:param name="number"/>
    <xsl:param name="title"/>
    <xsl:param name="plannedUnits"/>
    <xsl:param name="doneUnits"/>


    <Table Style="{{StaticResource NoBorderTable}}" Margin="0,10,0,10" Background="#EEEEEE">
      <Table.Columns>

        <TableColumn/>
        <TableColumn Width="100" />
      </Table.Columns>
      <TableRowGroup>

        <TableRow>


          <TableCell Style="{{StaticResource NoBorderCell}}">
            <Table Style="{{StaticResource NoBorderTable}}" Margin="0">
              <TableRowGroup>
                <TableRow>


                  <TableCell Style="{{StaticResource NoBorderCell}}" Padding="0">

                    <xsl:if test="$number">
                      <Paragraph FontSize="12">
                        <Bold>
                          [<xsl:value-of select="format-number($number,'000','pt-br-Decimal')"/>]
                          <xsl:value-of select="$title"/>
                        </Bold>
                      </Paragraph>
                    </xsl:if>


                    <Table Style="{{StaticResource NoBorderTable}}" Margin="0,3,0,10">
                      <Table.Columns>
                        <TableColumn Width="80"/>
                      </Table.Columns>
                      <TableRowGroup>
                        <TableRow>
                          <TableCell  Style="{{StaticResource NoBorderCell}}"/>
                          <xsl:for-each select="$showUnits">
                            <TableCell  Style="{{StaticResource DoubleLineCell}}">
                              <Paragraph  TextAlignment="Right">
                                <Bold>
                                  <xsl:value-of select="UnitName"/>
                                </Bold>
                              </Paragraph>
                            </TableCell>
                          </xsl:for-each>

                        </TableRow>
                        <TableRow>
                          <TableCell  Style="{{StaticResource NoBorderCell}}">
                            <Paragraph TextAlignment="Right">
                              <Bold>Planned</Bold>
                            </Paragraph>
                          </TableCell>
                          <xsl:for-each select="$showUnits">
                            <xsl:variable name="unitName" select="UnitName"/>
                            <xsl:variable name="planned">
                              <xsl:choose>
                                <xsl:when test="not(sum($plannedUnits[UnitName=$unitName]/Quantity))">
                                  <xsl:value-of select="number(0)"/>
                                </xsl:when>
                                <xsl:otherwise>
                                  <xsl:value-of select="sum($plannedUnits[UnitName=$unitName]/Quantity)"/>
                                </xsl:otherwise>
                              </xsl:choose>
                            </xsl:variable>
                            <TableCell  Style="{{StaticResource SingleLineCell}}">
                              <Paragraph TextAlignment="Right">
                                <xsl:value-of select="format-number($planned,'#.##0,00','pt-br-Decimal')"/>
                              </Paragraph>
                            </TableCell>
                          </xsl:for-each>
                        </TableRow>
                        <TableRow>
                          <TableCell  Style="{{StaticResource NoBorderCell}}">
                            <Paragraph  TextAlignment="Right">
                              <Bold>Burned</Bold>
                            </Paragraph>
                          </TableCell>
                          <xsl:for-each select="$showUnits">
                            <xsl:variable name="unitName" select="UnitName"/>
                            <xsl:variable name="effort">
                              <xsl:choose>
                                <xsl:when test="not(sum($doneUnits[UnitName=$unitName]/Effort))">
                                  <xsl:value-of select="number(0)"/>
                                </xsl:when>
                                <xsl:otherwise>
                                  <xsl:value-of select="sum($doneUnits[UnitName=$unitName]/Effort)"/>
                                </xsl:otherwise>
                              </xsl:choose>
                            </xsl:variable>
                            <TableCell  Style="{{StaticResource SingleLineCell}}">
                              <Paragraph  TextAlignment="Right">
                                <xsl:if test="$effort &gt; 0">-</xsl:if>
                                <xsl:value-of select="format-number($effort,'#.##0,00','pt-br-Decimal')"/>
                              </Paragraph>
                            </TableCell>
                          </xsl:for-each>
                        </TableRow>
                        <TableRow>
                          <TableCell  Style="{{StaticResource NoBorderCell}}">
                            <Paragraph  TextAlignment="Right">
                              <Bold>Balance</Bold>
                            </Paragraph>
                          </TableCell>
                          <xsl:for-each select="$showUnits">
                            <xsl:variable name="unitName" select="UnitName"/>
                            <xsl:variable name="planned">
                              <xsl:choose>
                                <xsl:when test="not(sum($plannedUnits[UnitName=$unitName]/Quantity))">
                                  <xsl:value-of select="number(0)"/>
                                </xsl:when>
                                <xsl:otherwise>
                                  <xsl:value-of select="sum($plannedUnits[UnitName=$unitName]/Quantity)"/>
                                </xsl:otherwise>
                              </xsl:choose>
                            </xsl:variable>
                            <xsl:variable name="effort">
                              <xsl:choose>
                                <xsl:when test="not(sum($doneUnits[UnitName=$unitName]/Effort))">
                                  <xsl:value-of select="number(0)"/>
                                </xsl:when>
                                <xsl:otherwise>
                                  <xsl:value-of select="sum($doneUnits[UnitName=$unitName]/Effort)"/>
                                </xsl:otherwise>
                              </xsl:choose>
                            </xsl:variable>
                            <xsl:variable name="balance" select="$planned - $effort"/>
                            <TableCell Style="{{StaticResource NoBorderCell}}">
                              <Paragraph  TextAlignment="Right" FontSize="12">
                                <Bold>
                                  <xsl:value-of select="format-number($balance,'#.##0,00','pt-br-Decimal')"/>
                                </Bold>

                              </Paragraph>
                            </TableCell>
                          </xsl:for-each>

                        </TableRow>
                      </TableRowGroup>
                    </Table>

                  </TableCell>

                </TableRow>
              </TableRowGroup>
            </Table>
          </TableCell>

          <TableCell Style="{{StaticResource NoBorderCell}}">
            <Table Style="{{StaticResource NoBorderTable}}" Margin="0">
              <TableRowGroup>
                <TableRow>


                  <xsl:variable name="plannedTotal" select="sum($plannedUnits[Quantity &gt; 0]/Quantity)"/>
                  <xsl:variable name="effortTotal" select="sum($doneUnits[Effort &gt; 0]/Effort)"/>

                  <TableCell Style="{{StaticResource NoBorderCell}}">
                    <xsl:call-template name="ItemBalanceScore">
                      <xsl:with-param name="score" select="$plannedTotal - $effortTotal"/>
                    </xsl:call-template>
                  </TableCell>
                </TableRow>
              </TableRowGroup>
            </Table>
          </TableCell>

        </TableRow>
      </TableRowGroup>
    </Table>
  </xsl:template>



</xsl:stylesheet>

